package com.cskaoyan.javase.practice.stage8.utils;

import com.cskaoyan.javase.practice.stage8.entity.Student;

import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;

import static com.cskaoyan.javase.practice.stage8.utils.AnnoUtils.*;

/**
 * @program: little-practice
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-19 21:50
 **/

public class ScannerUtils {
    private static Scanner sc = new Scanner(System.in);

    private ScannerUtils() {

    }

    // 已有id新建学生对象
    public static Student getInstanceWithIdByScanner(int id)
            throws ClassNotFoundException, NoSuchMethodException,
            InvocationTargetException, InstantiationException,
            IllegalAccessException, NoSuchFieldException {
        Student stu = ReflectionUtils.createStudentByReflectuon();
        stu.setStuId(id);
        stu.setName(getNameByScanner());
        stu.setAge(getAgeByScanner());
        stu.setGender(getGenderByScanner());
        stu.setGrade(getGradeByScanner());
        stu.setTele(getTeleByScanner());
        return stu;
    }

    // 接收id 并检验合法性
    public static int getIdByScanner() {
        int id = -1;
        while (true) {
            System.out.print("请输入正整数学生ID： ");
            try {
                id = Integer.parseInt(sc.nextLine());
            } catch (NumberFormatException nfe) {
                continue;
            }
            if (checkLegalId(id)) break;
        }
        return id;
    }

    // 接收姓名
    public static String getNameByScanner() throws NoSuchFieldException, ClassNotFoundException {
        String name = null;
        while (true) {
            System.out.print("请输入姓名(小于4位)： ");
            name = sc.nextLine().trim();
            if (checkLegalName(name)) break;
        }
        return name;
    }

    // 接收性别 并检验合法性
    public static String getGenderByScanner() throws NoSuchFieldException, ClassNotFoundException {
        String gender;
        while (true) {
            System.out.print("请输入性别(男/女)： ");
            gender = sc.nextLine().trim();
            if (checkLegalGender(gender)) break;
            System.out.println("输入格式不合法！");
        }
        return gender;
    }

    // 接收年龄 并检验合法性
    public static int getAgeByScanner() throws NoSuchFieldException, ClassNotFoundException {
        int age = -1;
        while (true) {
            System.out.print("请输入学生年龄(18-25)： ");
            try {
                age = Integer.parseInt(sc.nextLine());
            } catch (NumberFormatException nfe) {
                System.out.println("输入整数！");
                continue;
            }
            if (checkLegalAge(age)) break;
            System.out.println("输入格式不合法！");
        }
        return age;
    }

    // 接收年纪 并检验合法性
    public static String getGradeByScanner() throws NoSuchFieldException, ClassNotFoundException {
        String grade;
        while (true) {
            System.out.print("请输入班级（A, B, C, D）： ");
            grade = sc.nextLine().trim();
            if (checkLegalGrade(grade)) break;
            System.out.println("输入格式不合法！");
        }
        return grade;
    }

    // 接收电话 并检验合法性
    public static String getTeleByScanner() throws NoSuchFieldException, ClassNotFoundException {
        String tele = null;
        while (true) {
            System.out.println("8位电话： ");
            tele = sc.nextLine().trim();
            if (checkLegalTele(tele)) break;
            System.out.println("输入格式不合法！");
        }
        return tele;
    }

    // 接收菜单 并检验合法性
    public static int getMenuChoice(int range) {
        int choice = -1;
        while (true) {
            System.out.print("请选择序号（1-" + range + "）： ");
            try {
                choice = Integer.parseInt(sc.nextLine());
            } catch (NumberFormatException nfe) {
                continue;
            }
            if (checkLegalChoice(choice, range)) break;
            System.out.println("输入格式不合法！");
        }
        return choice;
    }

    // 接收确认标志 并检验合法性
    public static boolean getConfirmFlag() {
        int choice = -1;
        while (true) {
            System.out.print("1 确认；2 取消： ");
            try {
                choice = Integer.parseInt(sc.nextLine());
            } catch (NumberFormatException nfe) {
                continue;
            }
            if (checkLegalConfirm(choice)) break;
            System.out.println("输入格式不合法！");
        }
        return choice == 1;
    }

    // 检查id的合法性
    static boolean checkLegalId(int id) {
        return id > 0;
    }


    // 检查菜单选择的合法性
    static boolean checkLegalChoice(int choice, int range) {
        return choice > 0 && choice <= range;
    }

    // 检查确认选项的合法性
    static boolean checkLegalConfirm(int choice) {
        return choice == 1 || choice == 2;
    }
}
